package cn.ncu.wechat.vo;

import cn.ncu.wechat.model.MemberLevel;
import cn.ncu.wechat.util.StringUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class MemberLevelVo {
    private Long id;

    /**
     * 等级名称
     */
    private String name;

    /**
     * 等级图标
     */
    private String icon;

    /**
     * 所需最低积分
     */
    private Integer minPoints;

    /**
     * 折扣率
     */
    private BigDecimal discount;

    /**
     * 包含的权益ID列表
     */
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int[] benefits;

    /**
     * 状态:1启用,2禁用
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    //private Date updateTime;

    public void toVo(MemberLevel m){
        this.id = m.getId();
        this.name = m.getName();
        this.icon = m.getIcon();
        this.minPoints = m.getMinPoints();
        this.discount = m.getDiscount();
        this.status = m.getStatus();
        this.createTime = m.getCreateTime();
        //this.updateTime = m.getUpdateTime();
        if (StringUtil.IsNotNull(m.getBenefits())){
            String []benefits = m.getBenefits().split(",");
            this.benefits = new int[benefits.length];
            for (int i = 0; i < benefits.length; i++) {
                this.benefits[i] = Integer.parseInt(benefits[i]);
            }
        }
    }

    public MemberLevel VoToMember(){
        MemberLevel memberLevel = new MemberLevel();
        if (this.id != null){
            memberLevel.setId(this.id);
            memberLevel.setUpdateTime(new Date(System.currentTimeMillis()));
        }
        else memberLevel.setCreateTime(new Date(System.currentTimeMillis()));
        if (this.benefits!=null&&this.benefits.length!=0){
            StringBuilder s = new StringBuilder();
            for (int i : this.benefits) {
                s.append(i).append(",");
            }
            s.deleteCharAt(s.length() - 1);
            memberLevel.setBenefits(s.toString());
        }
         memberLevel.setName(this.name);
         memberLevel.setIcon(this.icon);
         memberLevel.setMinPoints(this.minPoints);
         memberLevel.setDiscount(this.discount);
         memberLevel.setStatus(this.status);
        return memberLevel;
    }
}
